
/*************************************************************************

Copyright 2010 Isuru Udana Loku Narangoda

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

**************************************************************************/

package remainingdegreedistribution;

import cytoscape.CyNetwork;
import cytoscape.Cytoscape;
import cytoscape.plugin.CytoscapePlugin;
import cytoscape.util.CytoscapeAction;
import giny.model.Node;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author ISURU UDANA LOKU NARANGODA
 * isudana@gmail.com
 */
public class RemainingDegreeDistribution extends CytoscapePlugin{

 public RemainingDegreeDistribution() {
        ViewRemainingDegreeMenuAction menuAction = new ViewRemainingDegreeMenuAction(this);
        Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) menuAction);
    }


    public class ViewRemainingDegreeMenuAction extends CytoscapeAction {

        public ViewRemainingDegreeMenuAction(RemainingDegreeDistribution myPlugin) {
            super("Show Remaining Degree Distribution");
            setPreferredMenu("View");
        }

    private void evaluate()  {
         /* Take Degree distribution function as h(k)= h
         *  h(k) = ( (k+1) * P(k+1) )/ <k>
         *  here <k> = sum of ( K * P(k) )
         *  P(k) = nodes in degree k / total nodes
         */

            // Get current network
            CyNetwork network = Cytoscape.getCurrentNetwork();

            HashMap<Integer, Integer> nodeDegreesAndCount = new HashMap<Integer, Integer>(); //To Store node degrees and their counts
            int maximumDegree = 0;                                                          //To hold Maximum degree

            Iterator it = network.nodesIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                Integer nodeDegree = network.getDegree(node);
                
                if (nodeDegreesAndCount.containsKey(nodeDegree)) {
                    nodeDegreesAndCount.put(nodeDegree, nodeDegreesAndCount.get(nodeDegree) + 1);
                } else {
                    nodeDegreesAndCount.put(nodeDegree, 1);

                    // Calculate the maximum degree
                    if ( nodeDegree > maximumDegree ) {
                        maximumDegree = nodeDegree;
                    }
                }
            }

            double[] p = new double[maximumDegree + 1]; // to hold probabilities
            double sumOfKP = 0d;                        // to hold <K>

            int totalNodes = network.getNodeCount();
                       
            Set degrees = nodeDegreesAndCount.keySet();

            for (Object deg : degrees) {
                Integer degree = (Integer) deg;
                Integer degreeCount = nodeDegreesAndCount.get(degree);

                 // Calculate P values
                p[degree] = degreeCount.floatValue() / totalNodes;     // P(k) = n(k) / n
                // Calculate <K>
                sumOfKP += p[degree] * degree.floatValue();
               
            }


            double[] q = new double[maximumDegree + 1];     // To hold Remaining Degree Distribution q(k)
            String[] xAxisLabels = new String[q.length];     // To hold X-axis labels

            //Calculate q value
            for (int i = 0; i < q.length-1; i++) {
                q[i] = p[i+1] * (i + 1d) / sumOfKP;
                xAxisLabels[i] = Integer.toString(i);
            }
            q[maximumDegree]=0;
            xAxisLabels[maximumDegree] = Integer.toString(maximumDegree);

            // Show the window
            JFrame window = new JFrame();
            window.setTitle("Remaining Degree Distribution of the Network "+network.getTitle());
            window.setSize(1000, 600);
            JPanel graph = new Graph(q, xAxisLabels);
            window.getContentPane().add(graph);
            window.setVisible(true);

    }

        @Override
        public void actionPerformed(ActionEvent ae) {
            evaluate();
        }

    }




}
